package com.iraeta;

import com.iraeta.common.Constants;
import com.iraeta.common.SecurityInterceptor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Scope;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import org.springframework.transaction.annotation.EnableTransactionManagement;
import org.springframework.web.multipart.MultipartResolver;
import org.springframework.web.multipart.commons.CommonsMultipartResolver;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import java.util.concurrent.Executor;
import java.util.concurrent.ThreadPoolExecutor;

@SpringBootApplication
//@PropertySource(value = {"classpath:config.properties"})
//@MapperScan(basePackages = {"com.iraeta.dao"})
@EnableTransactionManagement
public class TongBuWebApplication implements WebMvcConfigurer, ApplicationRunner {
	@Autowired
	Config config;

	public static void main(String[] args) {
		SpringApplication.run(TongBuWebApplication.class, args);
	}

	@Override
	public void run(ApplicationArguments args) throws Exception {
		System.out.println("TongBuWebApplication程序启动成功");
	}

	/*拦截器设置*/
	@Autowired
	SecurityInterceptor securityInterceptor;
	@Override
	public void addInterceptors(InterceptorRegistry registry) {
		registry.addInterceptor(securityInterceptor).addPathPatterns("/**")
				.excludePathPatterns("/static/**")
				.excludePathPatterns("/files/**");
	}

	/*静态资源设置*/
	@Override
	public void addResourceHandlers(ResourceHandlerRegistry registry) {
		registry.addResourceHandler(Constants.RESOURCE_PREFIX + "/**").addResourceLocations("file:" + config.profile + "/");
		//super.addResourceHandlers(registry);
	}

	/*跨域设置*/
	@Override
	public void addCorsMappings(CorsRegistry registry) {
		registry.addMapping("/**")
				.allowedMethods("*")
				.allowedOrigins("*")
				.allowedHeaders("*")
				//.allowCredentials(true)
				.maxAge(3600L);
	}

	/*文件上传设置 需要引入jar包*/
	@Bean
	public MultipartResolver multipartResolver() {
		CommonsMultipartResolver resolver = new CommonsMultipartResolver();
		resolver.setResolveLazily(true);
		resolver.setMaxInMemorySize(1024 * 1024);
		resolver.setMaxUploadSize(20 * 1024 * 1024);
		return resolver;
	}

	@Bean("cstaskexecutor")
	public Executor morePoolTaskExecutor(){
		ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
		executor.setCorePoolSize(15);	//核心线程数量
		executor.setMaxPoolSize(15);	//最大线程数量
		executor.setQueueCapacity(10);	//缓冲执行队列
		executor.setKeepAliveSeconds(60);	//线程空闲时间60秒
		executor.setThreadNamePrefix("tongbu");	//线程的前缀名字
		executor.setRejectedExecutionHandler(new ThreadPoolExecutor.DiscardOldestPolicy());
		return executor;
	}

	@Bean("requestItem")
	@Scope("request")
	public RequestItem getRequestItem() {
		return new RequestItem();
	}
}
